<template>
  <div class="dialog">
    <el-dialog
      title="添加工艺参数"
      :visible.sync="showDialog"
      width="45%"
      top="0"
      :close-on-click-modal="false"
      @close="cancel"
    >
      <el-form
        ref="ruleForm"
        label-position="left"
        :model="formData"
        :rules="rules"
        class="el-form"
      >
        <div
          v-for="(craftItem,idx) in formData.craftList"
          :key="idx"
          label-position="left"
          class="w100p formChildren"
        >
          <el-row>
            <el-col :span="12">
              <el-form-item
                label="工艺参数来源:"
                :prop="'craftList.' + idx + '.source'"
                class="w100p"
                :rules="[{ required: true, message: '请选择工艺工艺参数来源', trigger: ['blur'] }]"
              >
                <el-select
                  v-model="craftItem.source"
                  placeholder="请选择"
                  clearable
                >
                  <el-option
                    v-for="item in sourceOptions"
                    :key="item.value"
                    :label="item.label"
                    :value="item.value"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col v-if="craftItem.source === 0" :span="12">
              <el-form-item
                label="选择设备:"
                :prop="'craftList.' + idx +'.equipmentCode'"
                class="w100p"
                :rules="[{ required: true, message: '设备不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-select
                  v-model="craftItem.equipmentCode"
                  placeholder="请选择"
                  clearable
                  @change="changeEquip(craftItem.equipmentCode)"
                >
                  <el-option
                    v-for="item in equipOptions"
                    :key="item.value"
                    :label="item.equipmentName"
                    :value="item.equipmentCode"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="craftItem.source === 0" :span="12">
              <el-form-item
                label="选择设备数据:"
                :prop="'craftList.' + idx +'.dataItemName'"
                class="w100p"
                :rules="[{ required: true, message: '设备数据不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-select
                  v-model="craftItem.dataItemName"
                  placeholder="请选择"
                  clearable
                  @change="getcraftItem($event,idx)"
                >
                  <el-option
                    v-for="item in dataItemIdOptions"
                    :key="item.value"
                    :label="item.name"
                    :value="item.name"
                  />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col v-if="craftItem.source === 1" :span="12">
              <el-form-item
                label="工艺参数名称:"
                :prop="'craftList.' + idx +'.craftName'"
                class="w100p"
                :rules="[{ required: true, message: '工艺参数名称不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-input
                  v-model="craftItem.craftName"
                  clearable
                  placeholder="请输入工艺参数名称"
                />
              </el-form-item>
            </el-col>
            <el-col v-if="craftItem.source === 1" :span="12">
              <el-form-item
                label="单位"
                :prop="'craftList.' + idx +'.unitNo'"
                class="w100p"
                :rules="[{ required: true, message: '单位不能为空', trigger: ['change', 'blur'] }]"
              >
                <el-select
                  v-model="craftItem.unitNo"
                  placeholder="请选择单位"
                  clearable
                  @change="getunitName($event,idx)"
                >
                  <el-option
                    v-for="item in unitOptions"
                    :key="item.value"
                    :label="item.unitName"
                    :value="item.unitNo"
                  />
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
        </div>
        <el-button @click="addCraftList">添加工艺参数</el-button>
        <el-button @click="delCraftList">删除工艺参数</el-button>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import EquipApi from "@/api/processing-management/basic-info-manage/equip-config-dataitem/index";
import processManageApi from "@/api/processing-management/basic-info-manage/process-manage/index";
export default {
  name: "",
  components: {},
  props: {
    productionProcessesNo: {
      type: String,
      default: ""
    },
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    unitOptions: {
      type: Array,
      default: () => []
    },
    editData: {
      type: Object,
      default: () => {}
    }
  },
  data() {
    return {
      dataItemIdOptions: [],
      equipOptions: [],
      // 选择的设备型号
      showDialog: this.show,
      // 展示
      formData: {
        craftList: [
          {
            source: "",
            dataItemId: "",
            craftName: "",
            unitNo: "",
            unitName: ""
          }
        ]
      },
      sourceOptions: [
        {
          value: 0,
          label: "设备数据采集"
        },
        {
          value: 1,
          label: "人工手动录入"
        }
      ],
      rules: {
        jobTemplateName: [
          {
            required: true,
            message: "请输入模板名称",
            trigger: ["change", "blur"]
          }
        ],
        existing: [
          {
            required: true,
            message: "请选择是否有工艺参数",
            trigger: ["change", "blur"]
          }
        ],
        craftName: [
          {
            required: true,
            message: "工艺参数名称不能为空",
            trigger: ["change", "blur"]
          }
        ],
        unitNo: [
          {
            required: true,
            message: "单位不能为空",
            trigger: ["change", "blur"]
          }
        ],
        personNo: [
          {
            required: true,
            message: "请选择作业人",
            trigger: ["change", "blur"]
          }
        ]
      }
    };
  },
  watch: {
    productionProcessesNo(v) {
      if (v) {
        processManageApi.queryEquipByNo(v).then(res => {
          if (res.code === 1) {
            this.equipOptions = res.data;
          }
        });
      }
    },
    show(v) {
      this.showDialog = v;
      this.formData = {
        craftList: [
          {
            source: "",
            dataItemId: "",
            craftName: "",
            unitNo: ""
          }
        ]
      };
    }
  },
  created() {},
  mounted() {},
  methods: {
    // getcraftName(e, index) {
    //   console.log(e, index);
    //   this.formData.craftList[index].craftName = e;
    // },
    getunitName(e, index) {
      this.unitOptions.forEach(item => {
        if (item.unitNo === e) {
          console.log(item);
          this.formData.craftList[index].unitName = item.unitName;
          this.formData.craftList[index].unitNo = item.unitNo;
          console.log(this.formData, "1");
        }
      });
    },
    getcraftItem(e, idx) {
      console.log(e, this.dataItemIdOptions, idx);
      this.dataItemIdOptions.forEach(item => {
        if (item.name === e) {
          this.formData.craftList[idx].dataItemId = item.value;
          this.formData.craftList[idx].craftName = e;
        }
      });
      console.log(this.formData);
    },
    // 添加工艺参数
    addCraftList() {
      this.formData.craftList.push({
        source: "",
        dataItemId: "",
        craftName: "",
        unitNo: ""
      });
    },
    // 删除工艺参数
    delCraftList() {
      if (this.formData.craftList.length === 1) {
        this.$message.error("已经是最少工艺参数了");
        return;
      }
      this.formData.craftList.pop();
    },
    // 改变设备类型
    changePdtProcess() {
      this.$emit("changePdtProcess", this.formData.productionProcessesNo);
    },
    changeEquip(val) {
      EquipApi.queryByEquip(val).then(res => {
        if (res.code === 1) {
          let list = [];
          res.data.forEach(item => {
            list.push({
              name: item.dataItemName,
              value: item.dataItemId
            });
          });
          this.dataItemIdOptions = list;
        }
      });
    },
    save(ruleForm) {
      this.$refs[ruleForm].validate(valid => {
        if (valid) {
          let a = { ...this.formData };
          this.$emit("addProcessP", a.craftList);
          this.cancel();
        }
      });
    },
    close() {
      this.cancel();
    },
    cancel() {
      this.$emit("closeDialog");
      // this.resetForm("ruleForm");
      this.formData = {
        craftList: [
          {
            source: "",
            dataItemId: "",
            craftName: "",
            unitNo: ""
          }
        ]
      };
    }
    // 重置验证
    // resetForm(formName) {
    //   this.$refs[formName].resetFields();
    // }
  }
};
</script>

  <style lang="scss" scoped>
::v-deep
  .el-dialog__wrapper
  .el-dialog
  .el-dialog__body
  .el-form
  .el-form-item
  .el-form-item__label {
  min-width: 160px !important;
}
.formChildren {
  border: 1px dashed #0ea8c4;
  padding-top: 12px;
  margin-bottom: 12px;
}
</style>
